import { BaseDeployPanel } from "./BaseDeployPanel";
import { Language } from "../../../../../../extend/language/Language";
import { ItemExInfo } from "../../../../resource/info/ItemExInfo";
import { ObjCreate } from "../../../common/ObjCreate";
import { GImageBitmap } from "../../../../../../com/mui/controls/GImageBitmap";
import { DeployPanelItem } from "../DeployPanelItem";
import { FilterConst } from "../../../../../component/gconst/FilterConst";
import { PulseSharedObject } from "../../../player/PulseSharedObject";
import { TodayNoTipsConst } from "../../../../../component/gconst/TodayNoTipsConst";
import { ItemsUtil } from "../../../shortcuts/item/ItemsUtil";
import { Alert } from "../../../../../../com/mui/controls/Alert";
import { CheckBoxWin } from "../../../common/alertwins/CheckBoxWin";
import { MsgManager } from "../../../../manager/MsgManager";
import { Cache } from "../../../../cache/Cache";
import { DataEvent } from "../../../../events/DataEvent";
import { EquipmentModuleConst } from "../../data/EquipmentModuleConst";
import { ItemData } from "../../../../resource/info/ItemData";
import { EBind } from "../../../../../../Message/Public/EBind";
import { ShopConfig } from "../../../../resource/ShopConfig";
import { ShopRule } from "../../../../rules/ShopRule";
import { Dispatcher } from "../../../../../mvc/core/Dispatcher";
import { EventName } from "../../../../mvc/EventName";
import { NetDispatcher } from "../../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../../mvc/ServerCommand";
import { EquipUpdateParams } from "../../data/EquipUpdateParams";
import { EPlayerItemPosType } from "../../../../../../Message/Public/EPlayerItemPosType";
import { EquipAdvanceConfig } from "../../../../resource/EquipAdvanceConfig";
import { ECategory } from "../../../../../../Message/Public/ECategory";
import { TItemWeapon } from "../../../../../../Message/Db/Tables/TItemWeapon";
import { TEquipMelt } from "../../../../../../Message/Db/Tables/TEquipMelt";
import { ItemConfig } from "../../../../resource/ItemConfig";
import { EItemUpdateResult } from "../../../../../../Message/Game/EItemUpdateResult";
type int = number;
//class EquipMeltPanel
    
    export  class EquipMeltPanel extends BaseDeployPanel
    {
        constructor()
        {
            this._useItems = [];
            this.posData = [{"x": 100, "y": 105, "label": Language.getString(61042), "canRemove": true, "noBoarder": true}, {"x": 208, "y": 105, "label": Language.getString(61042), "canRemove": true, "noBoarder": true}, {"x": 156, "y": 150, "label": Language.getString(61043), "canRemove": false, "noBoarder": true}, {"x": 156, "y": 245, "label": Language.getString(61044), "canRemove": false, "noBoarder": false}];
            this.buttonLabel = Language.getString(61045);
            this._hintText1 = Language.getString(61048);
            super();
            return;
        }

        /* internal  */setHighestAddStar(arg1: Object, arg2: Object, arg3: Object): void
        {
            var loc1=0;
            var loc2=0;
            var loc3=0;
            loc1 = 1;
            while (loc1 <= 6) 
            {
                loc2 = loc2 + arg2["addStar" + loc1];
                loc3 = loc3 + arg3["addStar" + loc1];
                ++loc1;
            }
            var loc4=loc2 >= loc3 ? arg2 : arg3;
            loc1 = 1;
            while (loc1 <= 6) 
            {
                arg1["addition" + loc1] = loc4["addition" + loc1];
                arg1["addStar" + loc1] = loc4["addStar" + loc1];
                arg1["ref_val" + loc1] = loc4["ref_val" + loc1];
                ++loc1;
            }
            arg1["ref_lock"] = loc4["ref_lock"];
            return;
        }

        /* internal  */setHighestJewelHole(arg1: Object, arg2: Object, arg3: Object): void
        {
            var loc1=0;
            var loc2=0;
            var loc3=0;
            var loc4=null;
            loc1 = 1;
            while (loc1 <= 3) 
            {
                loc2 = loc2 + (arg2["hole" + loc1] > 0 ? 1 : 0);
                loc3 = loc3 + (arg3["hole" + loc1] > 0 ? 1 : 0);
                ++loc1;
            }
            var loc5=loc2 >= loc3 ? arg2 : arg3;
            loc1 = 1;
            while (loc1 <= 3) 
            {
                arg1["hole" + loc1] = loc5["hole" + loc1] > 0 ? 1 : 0;
                ++loc1;
            }
            loc1 = 4;
            while (loc1 <= 6) 
            {
                loc4 = "hole" + loc1;
                arg1[loc4] = arg2[loc4] > 0 || arg3[loc4] > 0 ? 1 : 0;
                ++loc1;
            }
            return;
        }

        /* internal  */setHighestWuxing(arg1: Object, arg2: ItemExInfo, arg3: ItemExInfo): void
        {
            if (arg2.wx_color > arg3.wx_color) 
            {
                this.copyWuxingJson(arg1, arg2);
            }
            else if (arg2.wx_color != arg3.wx_color) 
            {
                this.copyWuxingJson(arg1, arg3);
            }
            else if (arg2.wx_color != 0) 
            {
                if (arg2.wx_level > arg3.wx_level) 
                {
                    this.copyWuxingJson(arg1, arg2);
                }
                else if (arg2.wx_level != arg3.wx_level) 
                {
                    this.copyWuxingJson(arg1, arg3);
                }
                else if (arg2.wx_exp >= arg3.wx_exp) 
                {
                    this.copyWuxingJson(arg1, arg2);
                }
                else 
                {
                    this.copyWuxingJson(arg1, arg3);
                }
            }
            return;
        }

        /* internal  */copyWuxingJson(arg1: Object, arg2: ItemExInfo): void
        {
            arg1["wx_color"] = arg2.wx_color;
            arg1["wx_level"] = arg2.wx_level;
            arg1["wx_energy"] = arg2.wx_energy;
            arg1["wx_lucky"] = arg2.wx_lucky;
            arg1["wx_exp"] = arg2.wx_exp;
            return;
        }

        /* internal  */setWuxingShengLing(arg1: Object, arg2: ItemExInfo, arg3: ItemExInfo): void
        {
            if (allItem[0].itemData.itemInfo.item.level >= allItem[1].itemData.itemInfo.item.level) 
            {
                arg1["sl_lv"] = arg2.sl_lv;
            }
            else 
            {
                arg1["sl_lv"] = arg3.sl_lv;
            }
            return;
        }

        protected /* override */ createChildren(): void
        {
            var loc2=null;
            var loc4=null;
            var loc5=null;
            super.createChildren();
            ObjCreate.createLabel(Language.getString(61037), 0, 66, 350, 25, flash.text.TextFormatAlign.CENTER, this, true, 16445696);
            var loc1=new GImageBitmap("EquipMerge_arrow.swf", 118, 127);
            this.addChild(loc1);
            loc2 = new GImageBitmap("equipment_successRate.png");
            loc2.x = 14;
            loc2.y = 98;
            successRateItem.addChild(loc2);
            successRateLabel.x = 33;
            successRateLabel.y = 122;
            addChild(successRateItem);
            var loc3=0;
            while (loc3 < this.posData.length) 
            {
                loc4 = this.posData[loc3];
                (loc5 = new DeployPanelItem(loc3, loc4.noBoarder)).posIndex = loc3;
                loc5.x = loc4["x"];
                loc5.y = loc4["y"];
                loc5.label = loc4["label"];
                if (loc4.canRemove) 
                {
                    loc5.mouseEnabled = true;
                    loc5.addEventListener(flash.events.MouseEvent.CLICK, this.onItemClick);
                }
                addChild(loc5);
                allItem.push(loc5);
                ++loc3;
            }
            confirmButton.label = this.buttonLabel;
            confirmButton.x = 140;
            confirmButton.y = 305;
            confirmButton.textField.filters = [FilterConst.glowFilter];
            confirmButton.styleName = "ButtonSpecial";
            addChild(confirmButton);
            confirmButton.addEventListener(flash.events.MouseEvent.CLICK, this.onButtonClick);
            return;
        }

        /* internal  */onItemClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.currentTarget as DeployPanelItem;
            this.moveOutByIndex(loc1.posIndex);
            return;
        }

        /* internal  */onButtonClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.onButtonClickImpl(arg1.target);
            return;
        }

        /* internal  */onButtonClickImpl(arg1): void
        {
            var loc1=null;
            if (!this.checkCanSmelt()) 
            {
                return;
            }
            var loc2=arg1;
            switch (loc2) 
            {
                case confirmButton:
                {
                    if (PulseSharedObject.isTodayNotTips(TodayNoTipsConst.EquipMeltTodayNoTips)) 
                    {
                        this.onConfirm();
                    }
                    else 
                    {
                        loc1 = Language.getStringByParam(61052, ItemsUtil.getItemName(allItem[0].itemData), ItemsUtil.getItemName(allItem[1].itemData), ItemsUtil.getItemName(allItem[3].itemData));
                        Alert.alertWinRenderer = CheckBoxWin;
                        Alert.show(loc1, Language.getString(69900), Alert.OK | Alert.CANCEL, null, this.onStartMeltAlertHandler);
                    }
                    break;
                }
            }
            return;
        }

        /* internal  */onStartMeltAlertHandler(arg1: int, arg2: Boolean): void
        {
            if (arg1 == Alert.OK) 
            {
                if (arg2) 
                {
                    PulseSharedObject.save(TodayNoTipsConst.EquipMeltTodayNoTips, true);
                }
                this.onConfirm();
            }
            return;
        }

        /* internal  */checkCanSmelt(): Boolean
        {
            if (!allItem[0].itemData || !allItem[1].itemData) 
            {
                MsgManager.showRollTipsMsg(Language.getString(61046));
                return false;
            }
            if (!allItem[2].itemData || !allItem[2].enabled) 
            {
                if (Cache.instance.equipment.isAutoBuy && allItem[2].itemData) 
                {
                    return true;
                }
                MsgManager.showRollTipsMsg(Language.getString(61047));
                return false;
            }
            if (!isEnoughMoney) 
            {
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CoinNotEnough, null, false, true, true));
                MsgManager.showRollTipsMsg(Language.getString(60121));
                return false;
            }
            return true;
        }

        /* internal  */autoBuyMaterial(arg1: ItemData): void
        {
            var loc1=null;
            var loc3=0;
            var loc4=null;
            var loc2=0;
            var loc5=0;
            var loc6=this._useItems;
            for(loc1 of loc6) 
            {
                loc2 = loc2 + loc1.itemAmount;
            }
            if (allItem[2].amount > loc2) 
            {
                loc3 = arg1.bind != EBind._EBindYes ? arg1.itemCode : arg1.itemInfo.item.codeUnbind;
                loc4 = ShopConfig.instance.getShopItemInfo(loc3, ShopRule.ShopMallMaterial);
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallBuyItem, {"shopItemInfo": loc4, "amount": allItem[2].amount - loc2, "callBack": this.onItemBuySuccess}));
            }
            return;
        }

        /* internal  */onItemBuySuccess(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            return;
        }

        /* internal  */serverBagDataChangehandler(arg1): void
        {
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, this.currentEquips, false, true));
            this.onButtonClickImpl(confirmButton);
            return;
        }

        /* internal  */onConfirm(): void
        {
            this.onConfirmImpl();
            return;
        }

        /* internal  */onConfirmImpl(): void
        {
            var loc2=null;
            var loc3=null;
            var loc1=[];
            var loc4=0;
            var loc5=this._useItems;
            for(loc2 of loc5) 
            {
                if (loc2.uid == null) 
                {
                    continue;
                }
                loc1.push(loc2.uid);
            }
            loc3 = new EquipUpdateParams();
            loc3.operationId = this._operationId;
            loc3.autoUpdate = false;
            loc3.equipPosType = EPlayerItemPosType._EPlayerItemPosTypeBag;
            loc3.equipUid = allItem[0].itemData.uid;
            loc3.equipUid2 = allItem[1].itemData.uid;
            loc3.materials = loc1;
            loc3.autoBuyTone = Cache.instance.equipment.isAutoBuy;
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_DoUpdateEvent1, loc3, false, true));
            return;
        }

        protected /* override */ updateStatus(): void
        {
            var loc1=null;
            var loc2=0;
            successRate = 100;
            if (allItem[0].itemData && allItem[1].itemData) 
            {
                loc1 = EquipAdvanceConfig.instance.getEquipMeltInfo(this.suit1, this.suit2);
                loc2 = loc1 ? loc1.cost : 0;
                this.showResultEquip(loc1);
            }
            else 
            {
                allItem[2].itemData = null;
                allItem[2].enabled = true;
                allItem[3].itemData = null;
                loc2 = 0;
            }
            if (_cost != loc2) 
            {
                _cost = loc2;
                dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CostChange, _cost, false, true));
            }
            return;
        }

        public /* override */ putInItem(arg1: ItemData, arg2: Boolean=false, arg3=null): void
        {
            var loc1=0;
            while (loc1 < 3) 
            {
                if (allItem[loc1].itemData && allItem[loc1].itemData.uid == arg1.uid) 
                {
                    this.moveOutByIndex(loc1);
                    return;
                }
                ++loc1;
            }
            var loc2=EquipAdvanceConfig.instance;
            if (arg1.category == ECategory._ECategoryEquip) 
            {
                if (!allItem[0].itemData && !allItem[1].itemData) 
                {
                    this._lastPutinIndex = 0;
                    allItem[0].itemData = arg1;
                }
                else if (!allItem[0].itemData && allItem[1].itemData) 
                {
                    if (allItem[1].itemData.type == arg1.type && loc2.getEquipMeltInfo((arg1.itemInfo.extend as TItemWeapon).suit, this.suit2)) 
                    {
                        this._lastPutinIndex = 0;
                        allItem[0].itemData = arg1;
                    }
                    else 
                    {
                        MsgManager.showRollTipsMsg(this._hintText1);
                        return;
                    }
                }
                else if (allItem[0].itemData && !allItem[1].itemData) 
                {
                    if (allItem[0].itemData.type == arg1.type && loc2.getEquipMeltInfo((arg1.itemInfo.extend as TItemWeapon).suit, this.suit1)) 
                    {
                        this._lastPutinIndex = 1;
                        allItem[1].itemData = arg1;
                    }
                    else 
                    {
                        MsgManager.showRollTipsMsg(this._hintText1);
                        return;
                    }
                }
                else 
                {
                    MsgManager.showRollTipsMsg(Language.getString(61049));
                }
            }
            this.updateStatus();
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, this.currentEquips, false, true));
            return;
        }

        /* internal  */checkHasBindEquip(): Boolean
        {
            var loc1=0;
            loc1 = 0;
            while (loc1 < 2) 
            {
                if (allItem[loc1].itemData) 
                {
                    if (allItem[loc1].itemData.itemInfo.item.bind == EBind._EBindYes || allItem[loc1].itemData.itemExInfo.bind == EBind._EBindYes) 
                    {
                        return true;
                    }
                }
                ++loc1;
            }
            loc1 = 0;
            while (loc1 < this._useItems.length) 
            {
                if (ItemsUtil.isBind(this._useItems[loc1])) 
                {
                    return true;
                }
                ++loc1;
            }
            return false;
        }

        /* internal  */showResultEquip(arg1: TEquipMelt): void
        {
            var loc4=null;
            var loc5=null;
            var loc6=null;
            var loc7=null;
            if (!arg1 || !allItem[0].itemData || !allItem[1].itemData) 
            {
                allItem[3].itemData = null;
                return;
            }
            var loc1=arg1.suitTarget;
            var loc2=allItem[0].itemData.itemInfo.item.type;
            var loc3;
            if (loc3 = ItemConfig.instance.getEquipByParams(loc1, loc2, EBind._EBindYes)) 
            {
                loc4 = new ItemData(loc3.item.code);
                loc5 = allItem[0].itemData.itemExInfo;
                loc6 = allItem[1].itemData.itemExInfo;
                (loc7 = new Object())["bind"] = EBind._EBindYes;
                loc7["durable"] = (loc4.itemInfo.extend as TItemWeapon).durable;
                loc7["prefix"] = Math.max(loc5["prefix"], loc6["prefix"]);
                this.setHighestStrengthen(loc7, loc5, loc6);
                this.setHighestAddStar(loc7, loc5, loc6);
                this.setHighestJewelHole(loc7, loc5, loc6);
                if (allItem[0].itemData.itemInfo.item.level >= 70 || allItem[1].itemData.itemInfo.item.level >= 70) 
                {
                    this.setHighestWuxing(loc7, loc5, loc6);
                }
                this.setWuxingShengLing(loc7, loc5, loc6);
                loc4.setItemExInfo(loc7);
                allItem[3].itemData = loc4;
            }
            else 
            {
                allItem[3].itemData = null;
            }
            return;
        }

        public moveOutByIndex(arg1: int): void
        {
            if (arg1 >= 0 && arg1 < allItem.length && allItem[arg1].itemData) 
            {
                allItem[arg1].itemData = null;
                this.updateStatus();
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, this.currentEquips, false, true));
            }
            return;
        }

        public /* override */ setAutoChooseMaterial(arg1: Object): void
        {
            this._useItems = arg1.useItems as Array<any>;
            if (allItem[0].itemData && allItem[1].itemData) 
            {
                if (this._useItems.length > 0) 
                {
                    allItem[2].itemData = this._useItems[0] as ItemData;
                    allItem[2].amount = arg1.amountNeed;
                    allItem[2].enabled = arg1.isEnough;
                }
                else 
                {
                    allItem[2].itemData = null;
                    allItem[2].enabled = true;
                }
            }
            return;
        }

        public /* override */ clear(): void
        {
            clearAllItem();
            this.updateStatus();
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, [], false, true));
            return;
        }

        public /* override */ onUpdateResult(arg1: int, arg2: Boolean, arg3, arg4: Array<any>): void
        {
            if (arg1 != EItemUpdateResult._EItemUpdateResultSuccess) 
            {
                this.clear();
                MsgManager.showRollTipsMsg(Language.getString(61051));
            }
            else 
            {
                this.clear();
                MsgManager.showRollTipsMsg(Language.getString(61050));
            }
            return;
        }

        /* internal  */get suit1(): int
        {
            return allItem[0].itemData ? (allItem[0].itemData.itemInfo.extend as TItemWeapon).suit : 0;
        }

        /* internal  */get suit2(): int
        {
            return allItem[1].itemData ? (allItem[1].itemData.itemInfo.extend as TItemWeapon).suit : 0;
        }

        /* internal  */get currentEquips(): Array<any>
        {
            var loc1=[];
            if (allItem[0].itemData) 
            {
                loc1.push(allItem[0].itemData);
            }
            if (allItem[1].itemData) 
            {
                loc1.push(allItem[1].itemData);
            }
            return loc1;
        }

        /* internal  */setHighestStrengthen(arg1: Object, arg2: ItemExInfo, arg3: ItemExInfo): void
        {
            var loc1=arg2.totalStrengthen >= arg3.totalStrengthen ? arg2 : arg3;
            arg1["strengthen"] = loc1.strengthen;
            arg1["totalStrengthen"] = loc1.totalStrengthen;
            arg1["currentStrengthen"] = loc1.currentStrengthen;
            arg1["perfectStrengthen"] = loc1.perfectStrengthen;
            arg1["cur_strthen"] = loc1.cur_strthen;
            return;
        }

        /* internal */ /* var */_operationId: int=14;

        /* internal */ /* var */_useItems: Array<any>;

        /* internal */ /* var */posData: Array<any>;

        /* internal */ /* var */buttonLabel: string;

        /* internal */ /* var */_hintText1: string;

        /* internal */ /* var */_lastPutinIndex: int=0;
    }
